package hflyeer.gitee.com.global;

import hflyeer.gitee.com.entity.Employee;
import hflyeer.gitee.com.entity.Log;
import hflyeer.gitee.com.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class LogAspect {

    @Autowired
    private LogService logService;

    /**
     * 系统出错进行增强
     *
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(throwing="e",pointcut = "execution(* hflyeer.gitee.com..*(..))")
    public void sysLogAdvisor(JoinPoint joinPoint,Throwable e)  {
        String module = joinPoint.getTarget().getClass().getSimpleName();
        String ope = joinPoint.getSignature().getName();
        HttpServletRequest request = (HttpServletRequest)joinPoint.getArgs()[0];
        Employee employee = (Employee) request.getSession().getAttribute("USER");
        Log log = new Log();
        log.setModule(module);
        log.setOperation(ope);
        log.setOperator(employee.getAccount());
        log.setResult(e.getClass().getCanonicalName());
        logService.addSysLog(log);
    }

    /**
     * 操作增强
     * @param joinPoint
     */
//    @AfterReturning("execution(* hflyeer.gitee.com.*.*(..)) && !execution(* hflyeer.gitee.com.controller.SelfController.*(..)) && !execution(* hflyeer.gitee.com.controller.SelfController.to*(..))")
    @AfterReturning("execution(* hflyeer.gitee.com.controller..*(..)) && !execution(* hflyeer.gitee.com.controller.SelfController.*(..)) && !execution(* hflyeer.gitee.com.controller..to*(..))")
    public void opeLogAdvisor(JoinPoint joinPoint){
        String module = joinPoint.getTarget().getClass().getSimpleName();
        String ope = joinPoint.getSignature().getName();
        HttpServletRequest request = (HttpServletRequest)joinPoint.getArgs()[0];
        Employee employee = (Employee) request.getSession().getAttribute("USER");
        Log log = new Log();
        log.setModule(module);
        log.setOperation(ope);
        log.setOperator(employee.getAccount());
        log.setResult("成功");
        logService.addOpeLog(log);
    }

    /**
     * 登入增强
     * @param joinPoint
     */
    @After("execution(* hflyeer.gitee.com.controller.SelfController.login(..))")
    public void loginLogAdvisor(JoinPoint joinPoint){
        log(joinPoint);
    }

    /**
     * 登出增强
     * @param joinPoint
     */
    @Before("execution(* hflyeer.gitee.com.controller.SelfController.logout(..))")
    public void logoutLogAdvisor(JoinPoint joinPoint){
        log(joinPoint);
    }

    private void log(JoinPoint joinPoint) {
        String module = joinPoint.getTarget().getClass().getSimpleName();
        String ope = joinPoint.getSignature().getName();
        HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
        Employee employee = (Employee) request.getSession().getAttribute("USER");
        Log log = new Log();
        log.setModule(module);
        log.setOperation(ope);
        if (employee == null) {
            String username = request.getParameter("username");
            log.setOperator(username);
            log.setResult("失败");
        } else {
            log.setOperator(employee.getAccount());
            log.setResult("成功");
        }
        logService.addLoginLog(log);
    }
}
